package org.amphiprion.whohavethekids;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class ConfigureActivity extends Activity {

	private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

	/** Called when the activity is created */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// If the user closes window, don't create the widget
		setResult(RESULT_CANCELED);

		// Find widget id from launching intent
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
		}

		// If they gave us an intent without the widget id, just bail.
		if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
			Log.e("Napply", "Configuration Activity: no appwidget id provided");
			finish();
		}

		// Make sure we pass back the original appWidgetId before closing the activity
		setContentView(R.layout.activity_main);
		final Spinner cbType = (Spinner) findViewById(R.id.cbWidgetType);
		final EditText txtNbDate = (EditText) findViewById(R.id.txtWidgetNb);
		Button btOk = (Button) findViewById(R.id.btOk);
		btOk.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final Intent resultValue = new Intent();
				resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
				resultValue.putExtra("WIDGET_TYPE_INDEX", cbType.getSelectedItemPosition());

				int nbDatePerChild = 1;
				try {
					nbDatePerChild = Integer.parseInt(txtNbDate.getText().toString());
				} catch (Throwable e) {
					e.printStackTrace();
				}
				configureWidget(getApplicationContext(), cbType.getSelectedItemPosition(), nbDatePerChild);
				// Log.d("org.amphiprion.whohavethekids", "mAppID=" + mAppWidgetId);
				setResult(RESULT_OK, resultValue);
				finish();
			}
		});

	}

	/**
	 * Configures the created widget
	 * 
	 * @param context
	 */
	public void configureWidget(Context context, int widgetTypeIndex, int nbDatePerChild) {
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = sharedPreferences.edit();
		editor.putInt("WIDGET_TYPE_INDEX_FOR_" + mAppWidgetId, widgetTypeIndex);
		editor.putInt("WIDGET_NB_DATE_FOR_" + mAppWidgetId, nbDatePerChild);
		editor.commit();

		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		NextPresenceWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
	}
}
